package org.queryexecutor.swt.frames;

import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.SWT;
import java.awt.Frame;
import org.eclipse.swt.awt.SWT_AWT;
import java.awt.Panel;
import java.awt.BorderLayout;
import javax.swing.JRootPane;
import org.eclipse.swt.layout.GridData;
import org.queryexecutor.database.QueryTableModel;

import javax.swing.JTable;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import javax.swing.JPanel;
import java.awt.GridBagLayout;
import javax.swing.JScrollPane;
import java.awt.GridBagConstraints;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import org.eclipse.swt.custom.ScrolledComposite;

public class MainShell
{
	
	private Shell	sShell	= null;
	private QueryTableModel qtm;
	private Display display;
	private Text txtQuery;
	private JTable table;
	
	public MainShell()
	{
		this.qtm = new QueryTableModel();
		this.display = Display.getDefault();
	}
	
	public void show()
	{
		createSShell();
		this.sShell.open();
		while(!this.sShell.isDisposed()){
			if(!this.display.readAndDispatch()){
				this.display.sleep();
			}
		}
		this.display.dispose();
	}
	
	/**
	 * This method initializes sShell
	 * @wbp.parser.entryPoint
	 */
	private void createSShell()
	{
		this.sShell = new Shell();
		this.sShell.setText("Shell");
		this.sShell.setSize(new Point(300, 200));
		GridLayout gl_sShell = new GridLayout();
		this.sShell.setLayout(gl_sShell);
		
		Composite composite = new Composite(this.sShell, SWT.EMBEDDED);
		composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		
		Frame frame = SWT_AWT.new_Frame(composite);
		
		Panel panel = new Panel();
		frame.add(panel);
		panel.setLayout(new BorderLayout(0, 0));
		
		JRootPane rootPane = new JRootPane();
		panel.add(rootPane);
		
		JPanel panel_1 = new JPanel();
		rootPane.getContentPane().add(panel_1, BorderLayout.CENTER);
		panel_1.setLayout(new BorderLayout(0, 0));
		
		JScrollPane scrollPane = new JScrollPane();
		panel_1.add(scrollPane, BorderLayout.CENTER);
		
		this.table = new JTable(this.qtm);
		this.table.setFillsViewportHeight(true);
		this.table.setDoubleBuffered(true);
		this.table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		scrollPane.setViewportView(this.table);
		
		Composite composite_1 = new Composite(this.sShell, SWT.NONE);
		composite_1.setLayout(new GridLayout(2, false));
		composite_1.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
		
		Button btnQuery = new Button(composite_1, SWT.NONE);
		btnQuery.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				MainShell.this.qtm.initDB();
				MainShell.this.qtm.setQuery(MainShell.this.txtQuery.getText());
			}
		});
		btnQuery.setText("Query");
		
		this.txtQuery = new Text(composite_1, SWT.BORDER);
		this.txtQuery.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
	}
	
	public void query(String query)
	{
		this.qtm.setQuery(query);
	}
}
